package hydrap2p.modulemanager;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class StreamCopier extends Thread {

	private InputStream in;
	private OutputStream out;
	private ModuleStatusLocal parent;
	private boolean running;
	
	public StreamCopier(ModuleStatusLocal parent, String name, InputStream in, OutputStream out)
	{
		this.in=in;
		this.out=out;
		this.parent=parent;
		setName(name);
	}
	
	public void run()
	{
		int read=0;
		byte[] temp=new byte[1024];
		running=true;
		while (read>=0 && running)
		{
			try {
				read=in.read(temp);
				if (read>=0)
					out.write(temp, 0, read);
			} catch (IOException e) {
				
			}
		}
		if (running)
			parent.processDied();
	}
	
	public void shutdown()
	{
		running=false;
		try {
			in.close();
			out.close();
		} catch (IOException e) {
			// Already closed I guess
		}
	}
}
